<?php 
	require("funkcie.php");
	/*session_start();
	if(!isset($_SESSION['success']) || $_SESSION['success'] != true) die("Pruser, nie si zalogovany");
		else {
			session_destroy();
	*/
	
	$sql = connectDB();

	if(isset($_GET["page"]) && $_GET["page"] > 0) $page = $_GET["page"]; else $page = 1;
	if(isset($_SESSION["rpp"]) && $_SESSION["rpp"] > 0) $count = $_SESSION["rpp"]; else $count = 30;
	
	
	function createQueryForTable($id, $loc, $page, $count) {
		switch($id) {
			case 1: {
				$query = <<<END
				SELECT devices1.id AS Id, devices1.name AS Meno, devices1.ip as IP, mac AS MAC, to_port AS Zasuvka,
				devices2.name AS "Zapojene do", locations.name AS Lokalita, place AS Poloha,
				devices2.id AS switchid, locations.id AS locationid
				FROM devices devices1, devices devices2, pc, locations, links
				WHERE devices1.id = pc.device_id AND devices1.type = 1
				AND devices1.location = locations.id AND devices1.id = links.to AND devices2.id = links.from
END;
				break;
			}
			case 2: {
				$query = <<<END
				SELECT devices1.id AS Id, devices1.name AS Meno, devices1.ip as IP, to_port AS Zasuvka,
				devices2.name AS "Zapojene do", locations.name AS Lokalita, place AS Poloha,
				devices2.id AS switchid, locations.id AS locationid
				FROM devices devices1, devices devices2, printer, locations, links
				WHERE devices1.id = printer.device_id AND devices1.type = 1
				AND devices1.location = locations.id AND devices1.id = links.to AND devices2.id = links.from
END;
				break;
			}
			case 3: {
				$query = <<<END
				SELECT devices1.id AS Id, devices1.name AS Meno, ip AS IP, numports AS "Pocet portov", locations.name AS Lokalita, 
				rack AS Rack, locations.id AS locationid
				FROM devices devices1, switch, locations
				WHERE devices1.id = switch.device_id AND devices1.type = 3
				AND devices1.location = locations.id 
END;
				break;
			}
			default: return false;
		}
		
		if($loc > 0) $query .= "\n\t\t\t\t AND devices1.location = {$loc}";
		$query .= "\n\t\t\t\tLIMIT ".($page-1)*$count.", ".$count;
		return $query;
	}
	
	function tableHead($columns) {
		if(!is_array($columns)) return false;
		
		$output = "\t\t\t<tr>\n";
		foreach($columns as $value) {
			$output .= "\t\t\t\t<th>".$value."</th>\n";
		}
		$output .= "\t\t\t</tr>\n";
		return $output;
	}
	
	function tableRows($res, $id, $page, $count) {
		
		switch($id) {
			case 1: {
				$i = ($page-1)*$count + 1;
				while($row = mysql_fetch_assoc($res)) {
					echo <<<END
			<tr>
				<td><input type='checkbox' name = "checkbox[]" value = {$row["Id"]}/></td>
				<td>{$i}.</td>
				<td><a href="device.php?id={$row["Id"]}">{$row["Meno"]}</a></td>
				<td><a href="device.php?id={$row["Id"]}">{$row["IP"]}</a></td>
				<td>{$row["MAC"]}</td>
				<td>{$row["Zasuvka"]}</td>
				<td><a href="device.php?id={$row["switchid"]}">{$row["Zapojene do"]}</a></td>
				<td><a href="devices.php?type={$_GET["type"]}&loc={$row["locationid"]}">{$row["Lokalita"]}</a></td>
				<td>{$row["Poloha"]}</td>
			</tr>\n
END;
					$i++;
				}
				break;
			}
			case 2: {
				$i = ($page-1)*$count + 1;
				while($row = mysql_fetch_assoc($res)) {
					echo <<<END
			<tr>
				<td><input type='checkbox' name = "checkbox[]" value = {$row["Id"]}/></td>
				<td>{$i}.</td>
				<td><a href="device.php?id={$row["Id"]}">{$row["Meno"]}</a></td>
				<td><a href="device.php?id={$row["Id"]}">{$row["IP"]}</a></td>
				<td>{$row["Zasuvka"]}</td>
				<td><a href="device.php?id={$row["switchid"]}">{$row["Zapojene do"]}</a></td>
				<td><a href="devices.php?type={$_GET["type"]}&loc={$row["locationid"]}">{$row["Lokalita"]}</a></td>
				<td>{$row["Poloha"]}</td>
			</tr>\n
END;
					$i++;
				}
				break;
			}
			case 3: {
				$i = ($page-1)*$count + 1;
				while($row = mysql_fetch_assoc($res)) {
					echo <<<END
			<tr>
				<td><input type='checkbox' name = "checkbox[]" value = {$row["Id"]}/></td>
						<td>{$i}.</td>
						<td><a href="device.php?id={$row["Id"]}">{$row["Meno"]}</a></td>
						<td><a href="device.php?id={$row["Id"]}">{$row["IP"]}</a></td>
						<td>{$row["Pocet portov"]}</td>
						<td><a href="devices.php?type={$_GET["type"]}&loc={$row["locationid"]}">{$row["Lokalita"]}</a></td>
						<td>{$row["Rack"]}</td>
				</tr>\n
END;
					$i++;
				}
				break;
			}
			default: return false;
		}
	}

	function generateTable($id, $loc, $page, $count) {
		if($page < 1) $page = 1;
		if($count < 1) $count = 30;
		
		$query = createQueryForTable($id, $loc, $page, $count);
		if(!$query) die("Chyba pri vytvarani dopytu!");
		$res = mysql_query($query);
		if(!$res) die("Chyba pri citani z db!");
		
		$ths = array("&nbsp;");
		switch($id) {
			case 1: {$off = 2; break; }
			case 3: {$off = 1; break; }
			//default: $off = 2;
		}
		for ($i=0; $i < (mysql_num_fields($res) - $off); $i++ ) {
			array_push($ths, mysql_field_name($res, $i));
		}
		$rows = mysql_num_rows($res);
		$from = ($page - 1)*$count + 1;
		$to = $page*$count;
		if($to > $rows) $to = $rows;
		echo <<<END
		
		<p class="page">Zobrazujem stranu: {$page}, polozky: {$from} - {$to}</p>
		<table class="content">

END;
		echo tableHead($ths);
		echo tableRows($res, $id, $page, $count);
		echo "\t\t</table>";
		
		return $rows;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dth XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/Dth/xhtml1-transitional.dth">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="author" content="matuldo" />
	<link rel="stylesheet" type="text/css" href="hpinfo.css" />
	<script type="text/javascript" src="prototype.js"></script>
	<script type="text/javascript" src="fcie.js"></script>

	<title>HP INFO</title>
</head>

<body>

<div class="main">

	<!-- Top menu -->
	<div class="top_menu">
		<img src="./Images/logo2.jpg" alt="HP INFO"/>
	</div>


	<!-- Left menu -->
	<div class="left_menu">
		<ul id="menu">
		<?php
		generateLeftMenu();
		?>
		</ul>
	</div>

	<!-- Content window -->
	<fieldset class="content" id="content">
	<legend>
	&nbsp;
	<?php
		if(isset($_GET["type"]) && $_GET["type"] != "")
			echo "<a href='devices.php?type=".$_GET["type"]."'>".getName("devicetypes",$_GET["type"])."</a>";
		if(isset($_GET["loc"]) && $_GET["loc"] != "")
			echo " - <a href='devices.php?type=".$_GET["type"]."&loc=".$_GET["loc"]."'>".getName("locations",$_GET["loc"])."</a>";
	?>
	
	&nbsp;
	</legend>
	<?php
		echo '	<div>
			
		</div>';

			if(isset($_GET["type"]) && $_GET["type"] != "") {
				$rows = generateTable($_GET["type"], $_GET["loc"], $page, $count);
			}
		
		
		echo "\n\t\t<div class=\"page\">";
				$pagecount = 10 /*ceil($rows/$count)*/;
				if($pagecount > 1) {
					if($page > 1) { 
						echo "\t\t\t<a href=\"devices.php?type={$_GET["type"]}&loc={$_GET["loc"]}&page=1\"><<</a>\n";
						echo "\t\t\t&nbsp;<a href=\"devices.php?type={$_GET["type"]}&loc={$_GET["loc"]}&page=".($page - 1)."\"><</a>&nbsp;\n";
					}
					$from = (($page - 3) > 0)?($page - 3):1;
					$to = (($page + 3) <= $pagecount)?($page + 3):($pagecount);
					for($i = $from; $i <= $to; $i++)
						if($i == $page) echo " {$i}\n";
						else echo "\t\t\t<a href=\"devices.php?type={$_GET["type"]}&loc={$_GET["loc"]}&page={$i}\">{$i}</a>\n";
					
					if($page < $pagecount) {
						echo "\t\t\t&nbsp;<a href=\"devices.php?type={$_GET["type"]}&loc={$_GET["loc"]}&page=".($page + 1)."\">></a>\n";
						echo "\t\t\t&nbsp;<a href=\"devices.php?type={$_GET["type"]}&loc={$_GET["loc"]}&page={$pagecount}\">>></a>&nbsp;\n";
					}
					echo <<<END
\t\t\t<select onchange="location = 'devices.php?type={$_GET["type"]}&loc={$_GET["loc"]}&page='+this.options[this.selectedIndex].value;">

END;
					for($i = 1; $i <= $pagecount; $i++)
						echo "\t\t\t\t<option ".($page==$i?"selected=\"yes\"":"")." value={$i}>{$i}</option>\n";
					echo "\t\t\t</select>\n";
				}
		echo "\t\t</div>\n";
	?>
	</fieldset>

</div>

</body>
</html>

<?php //} 
	disconnectDB($sql);
?>